package com.doge.service.admin.storage;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.log.Log;
import com.doge.bean.pojo.log.LogExample;
import com.doge.bean.pojo.role.RoleExample;
import com.doge.bean.pojo.storage.Storage;
import com.doge.bean.pojo.storage.StorageExample;
import com.doge.exception.Delete502Exception;
import com.doge.exception.Param602Exception;
import com.doge.mapper.StorageMapper;
import com.doge.util.file.FileUploadUtils;
import com.doge.util.properties.AliyunUtilProperties;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @program: doge
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-29 20:56
 **/

@Service
public class StorageServiceImpl implements StorageService{
    @Autowired
    StorageMapper mapper;
    @Autowired
    FileUploadUtils fileUpload;
    @Autowired
    AliyunUtilProperties aliyun;

    @Override
    public Storage create(MultipartFile file) throws Exception {
        // 获取 原始文件名name
        String name = file.getOriginalFilename();
        // 获取文件类型
        String type = file.getContentType();
        // 获取文件大小
        Integer size = (int) file.getSize();
        // 存储在本地硬盘，返回key
        String key = fileUpload.upload(file);
        // TODO: 拼接 url, url怎么生成？ 跟前台有什么关系？
        // url 拼接 阿里云
        //String url = "http://localhost:8083/wx/storage/fetch/"+key;
        String url = "https://"+aliyun.getOss().getBucket()+"."+aliyun.getOss().getEndPoint()+"/"+key;
        // 封装为 Storage类, 添加时间
        Storage storage = new Storage();
        storage.setKey(key);
        storage.setName(name);
        storage.setSize(size);
        storage.setType(type);
        storage.setUrl(url);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        // 插入到 storage 表中, 返回 id
        mapper.insertSelective(storage);
        return storage;
    }

    @Override
    public BaseData list(BaseParam param, String key, String name) {
        // 分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        // 模糊查询
        StorageExample example = new StorageExample();
        StorageExample.Criteria criteria = example.createCriteria().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(key)){
            criteria.andKeyLike("%"+key+"%");
        }
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        // 获得 List<Log>
        List<Storage> storages = mapper.selectByExample(example);
        // 封装返回
        PageInfo pageInfo = new PageInfo(storages);
        BaseData data = BaseData.list(storages, pageInfo.getTotal());
        data.setPages(pageInfo.getPages());
        data.setPage(param.getPage());
        data.setLimit(param.getLimit());
        return data;
    }

    @Override
    public void update(Storage storage){
        storage.setUpdateTime(new Date());
        // 选择更新用户: 只更新不为 null的列
        mapper.updateByPrimaryKeySelective(storage);
    }

    @Override
    public void delete(Storage storage) throws Exception {
        // 根据 id删除角色
        StorageExample example = new StorageExample();
        example.createCriteria().andIdEqualTo(storage.getId());
        // 逻辑删除，不是真实的删除
        storage.setDeleted(true);
        int i = mapper.updateByExampleSelective(storage,example);
        if (i!=1)
            throw new Delete502Exception("删除角色失败！");
    }
}
